package org.tnaggs.project.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.tnaggs.project.constants.Roles;
import org.tnaggs.project.manager.ContactManager;
import org.tnaggs.project.manager.LeagueManager;
import org.tnaggs.project.manager.LocationManager;
import org.tnaggs.project.model.Contact;
import org.tnaggs.project.model.League;
import org.tnaggs.project.model.Location;
import org.tnaggs.project.model.Role;
import org.tnaggs.project.security.ContactUser;
import org.tnaggs.project.view.LeagueView;
import org.tnaggs.project.view.LocationView;



@Controller
public class HomeController {

	@Autowired
	private LeagueManager laxManager;
	
	@Autowired
	private LocationManager locManager;
	
	@Autowired
	private ContactManager conManager;
	
	@RequestMapping("/index.*")
    public ModelAndView helloWorld() {
        String message = "Welcome to lacrosse app!";
        ModelAndView mav = new ModelAndView("index");
        mav.addObject("commandLeague", new League());
        mav.addObject("commandLocation", new Location());
        mav.addObject("commandContact", new Contact());
        mav.addObject("message",message);
        ArrayList<LeagueView> leagues = new ArrayList<LeagueView>();
        List<League> currLeagues = laxManager.getAllLeagues();
        for(League league : currLeagues) {
        	LeagueView view = new LeagueView();
        	view.setLeague(league);
        	view.setAdmin(conManager.getContact(league.getAdmin()));
        	leagues.add(view);
        }
        
        ArrayList<LocationView> locations = new ArrayList<LocationView>();
        List<Location> currLocals = locManager.getAllLocations();
        for(Location local : currLocals) {
        	LocationView view = new LocationView();
        	view.setLocation(local);
        	view.setContact(conManager.getContact(local.getContactID()));
        	locations.add(view);
        }
        
        ArrayList<String> seasons = new ArrayList<String>();
        seasons.add("Spring");
        seasons.add("Summer");
        seasons.add("Fall");
        seasons.add("Winter");
        
        ArrayList<Role> roles = new ArrayList<Role>();
        roles.add(new Role(Roles.ADMIN, 1));
        roles.add(new Role(Roles.COACH, 2));
        roles.add(new Role(Roles.PLAYER, 3));
        
        mav.addObject("roles", roles);
        mav.addObject("seasons", seasons);
        mav.addObject("locations", locations);
        mav.addObject("leagues",leagues);
        mav.addObject("contacts",conManager.getAllContacts());
        ContactUser user = null;
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal instanceof UserDetails){
          user = (ContactUser)principal;
        } 
        if(user != null) {
          mav.addObject("principal", user.getContact());
        } else {
          mav.addObject("principal", "anonymous");	
        }
        return mav;
    }
	
	
}
